/**
 * @author 徐楠
 * @date 2022/3/1 19:37
 * @version 1.0
 */

package com.xunan.likou;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class WrongCollection {
    public static void main(String[] args) {
        int[] nums = {2, 3, 2};
        int[] errorNums = findErrorNums(nums);
        for (int i = 0; i < errorNums.length; i++) {
            System.out.println(errorNums[i]);
        }

    }

    public static int[] findErrorNums(int[] nums) {
        int[] errorNums = new int[2];
        int n = nums.length;
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int num : nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        for (int i = 1; i <= n; i++) {
            int count = map.getOrDefault(i, 0);
            if (count == 2) {
                errorNums[0] = i;
            } else if (count == 0) {
                errorNums[1] = i;
            }
        }
        return errorNums;

    }

    public static int[] findErrorNumsPlus(int[] nums) {
        int[] errorNums = new int[2];
        //errorNums[0]记录错误的数字
        //errorNums[1]记录正确的数字
        int n = nums.length;
        Arrays.sort(nums);
        int prev = 0;
        for (int i = 0; i < n; i++) {
            int curr = nums[i];
            //记录错误的数字
            if (curr == prev) {
                errorNums[0] = prev;
            } else if (curr - prev > 1) {
                //curr - prev > 1比较错误数字是否是两个数之间的或他是开始时就错了
                errorNums[1] = prev + 1;
            }
            prev = curr;
        }
        //判断缺失的数字是最后一位数
        if (nums[n - 1] != n) {
            errorNums[1] = n;
        }
        return errorNums;
    }

}
